package main

import (
	"fmt"
	"sort"
)

var m = make(map[string]int)

func k(list []string) string {
	return fmt.Sprintf("%q", list)
}
func Add(list []string) {
	m[k(list)]++
}
func Count(list []string) int {
	return m[k(list)]
}
func main() {
	fmt.Println("-----map init--------------")
	ages := make(map[string]int)
	fmt.Println(ages)
	ages2 := map[string]int{
		"alice":   31,
		"charlie": 34,
	}
	ages3 := make(map[string]int)
	ages3["alice"] = 31
	ages3["charlie"] = 34

	ages4 := map[string]int{}
	fmt.Println(ages4)

	fmt.Println(ages2["alice"])
	delete(ages2, "alice")
	fmt.Println(ages2)
	delete(ages2, "unknown")
	ages2["bob"] += 1
	fmt.Println(ages2)
	fmt.Println("-------------------")
	for key, value := range ages2 {
		fmt.Printf("%s\t%d\n", key, value)
	}
	fmt.Println("---------map sort key----------")
	var names []string
	for name := range ages2 {
		names = append(names, name)
	}
	sort.Strings(names)
	for _, key := range names {
		fmt.Printf("%s\t%d\n", key, ages2[key])
	}
	fmt.Println("-----Is key  absent?--------------")
	_, ok := ages2["bobz"]
	if !ok {
		fmt.Println("bobz not exist")
	}
	if _, okz := ages2["bob"]; !okz {
		fmt.Println("bob not exists")
	} else {
		fmt.Println("bob exists")
	}
	/*fmt.Println("---------map as set----------")
	seen := make(map[string]bool)
	input := bufio.NewScanner(os.Stdin)
	for input.Scan() {
		line := input.Text()
		if !seen[line] {
			seen[line] = true
			fmt.Println(line)
		}
	}
	if err := input.Err(); err != nil {
		fmt.Fprintf(os.Stderr, "dedup: %v\n", err)
		os.Exit(1)
	}*/
	fmt.Println("-----------map string list sprintf----------")
	t := []string{"a", "b", "c"}
	Add(t)
	Add(t)
	Add(t)
	tt := []string{"b", "c"}
	Add(tt)
	Add(tt)
	Add([]string{"a", "c"})
	Add([]string{"a", "b"})
	Add([]string{"a"})
	Add([]string{"b"})
	Add([]string{"c"})
	fmt.Println(Count(t))
	fmt.Println(Count(tt))
	fmt.Println("----------map value 聚合-----------")
	addEdge("A", "B")
	addEdge("A", "C")
	addEdge("A", "D")
	fmt.Println(hasEdge("A", "B"))
	fmt.Println(hasEdge("A", "C"))
	fmt.Println(hasEdge("A", "E"))

}

var graph = make(map[string]map[string]bool)

func addEdge(from, to string) {
	edges := graph[from]
	if edges == nil {
		edges = make(map[string]bool)
		graph[from] = edges
	}
	edges[to] = true
}
func hasEdge(from, to string) bool {
	return graph[from][to]
}
